#!/bin/bash 
# 设置命令标题名称
echo -ne "\e]2;银河麒麟V10Sp1工具箱\a"

# 确保脚本在终端中运行
if [ -z "$TERM" ]; then
    # 如果没有终端，尝试启动一个终端来运行脚本
    x-terminal-emulator -e "$0"
    exit 0
fi

# 颜色定义
red='\033[0;31m' 
green='\033[0;32m' 
yellow='\033[0;33m'
blue='\033[0;34m' 
cyan='\033[0;36m'
magenta='\033[0;35m' 
reset='\033[0m'
nc='\033[0m'

# 优化图标路径检查
icon_path="/usr/share/icons/hicolor/48x48/apps/xq-kylin-tools.png"
[[ ! -f "$icon_path" ]] && icon_path=""  # 图标不存在时不显示

# ====================== 通知工具函数 ======================
# 桌面通知与终端提示函数
show_notification() { 
    local title="$1" message="$2" level="$3" icon="${4:-}"
    case "$level" in
        "success") color="$green"; urgency="normal" ;;
        "warning") color="$yellow"; urgency="normal" ;;
        "error")   color="$red"; urgency="critical" ;;
        *)         color="$nc"; urgency="normal" ;;
    esac
    echo -e "\n${color}【$title】${message}${NC}"
    # 发送桌面通知
    if command -v notify-send &> /dev/null; then
        if [[ -n "$icon" && -f "$icon" ]]; then
            notify-send -u "$urgency" -i "$icon" "$title" "$message" 2>/dev/null || true
        else
            notify-send -u "$urgency" "$title" "$message" 2>/dev/null || true
        fi
    fi
}

# ====================== 权限检查与获取函数 ======================
# 错误处理函数 - 防止闪退并提示用户
handle_error() {
    local message="$1"
    echo -e "\n${red}【错误】${message}${NC}"
    show_notification "操作失败" "${message}" "error" "$icon_path"
    read -p "按Enter键继续或关闭窗口..."  # 防止闪退，等待用户确认
}
echo -e "${blue}▶ 正在验证sudo权限，请输入您的密码...${NC}"

if sudo -v; then
    echo -e "\n${green}【成功】sudo权限验证通过，继续执行后续操作${NC}"
else
    echo -e "\n${red}【错误】sudo权限验证失败！原因可能是：${NC}" 
    echo -e "${yellow}1. 当前用户未配置sudo权限"
    echo -e "2. 密码输入错误"
    exit 1
fi

# 用户确认函数：提示用户确认操作（y/n），返回 0（确认）或 1（取消）
confirm_operation() {
    echo -en "${yellow}$1 (y/n): ${reset}"
    read -r response
    [[ "$response" =~ ^[Yy]$ ]] && return 0 || return 1  
}

# ====================== 6）其他工具菜单开始 ======================
# 其他工具菜单：处理sudo、远程桌面、U盘弹窗问题
other_menu() {
    while true; do
        clear
        echo -e "${blue}======================  银河麒麟工具箱  =====================${reset}"        
        echo -e "${green}╔══════════════════════════════════════════════════════════╗"
        echo -e "║                           其他工具                       ║"
        echo -e "╚══════════════════════════════════════════════════════════╝${reset}"             
        echo -e " ${magenta}1)${reset} sudo权限修复"
        echo -e " ${magenta}2)${reset} 远程桌面修复"
        echo -e " ${magenta}3)${reset} U盘弹窗修复"
        echo -e " ${magenta}4)${reset} WPS打印时显示多个修复"       
        echo -e " ${magenta}5)${reset} 解决WPS运行慢或卡"                
        echo -e " ${magenta}6)${reset} 强制删除软件"
        echo -e " ${magenta}7)${reset} 删除奇安信杀毒软件"        
        echo -e " ${red}0)${reset} 返回主菜单"
        echo -e "${blue}$(printf '═%.0s' $(seq 1 60))${reset}"
        echo -en "${yellow}请输入选项编号 [ 例如：1 ]: ${reset}"
        read -rp "" choice
        case $choice in
            1) fix_sudo_permission; echo -e "\n${yellow}按Enter键继续...${reset}"; read -r ;;
            2) fix_remote_desktop; echo -e "\n${yellow}按Enter键继续...${reset}"; read -r ;;
            3) fix_usb_device_popup; echo -e "\n${yellow}按Enter键继续...${reset}"; read -r ;;
            4) fix_wps_multiple_printers; echo -e "\n${yellow}按Enter键继续...${reset}"; read -r ;;     
            5) fix_wps_performance; echo -e "\n${yellow}按Enter键继续...${reset}"; read -r ;; 
            6) force_remove_software; echo -e "\n${yellow}按Enter键继续...${reset}"; read -r ;;   
            7) force_remove_qaxsafe; echo -e "\n${yellow}按Enter键继续...${reset}"; read -r ;;  
            0) echo -e "${green}→ 正在返回主菜单...${reset}";  break ;;
            *) echo -e "${red}✗ 无效选择，请重新输入！${reset}"; sleep 0.5 ;;
        esac
        echo -e "\n${yellow}按Enter键继续...${reset}"
        read -r
    done
}

# ====================== 其他工具功能子菜单 ======================
# sudo权限修复：重置sudoers配置，确保sudo权限正常
fix_sudo_permission() {
    echo -e "${cyan}>>> sudo权限修复${reset}"
    sudo chmod u+w /etc/sudoers
    sudo sed -i '/%sudo/c\%sudo ALL=(ALL:ALL) ALL' /etc/sudoers
    sudo chmod u-w /etc/sudoers
    echo -e "${green}[通知] sudo权限已修复${reset}"
}

# 远程桌面重复登录修复：修改xrdp端口配置
fix_remote_desktop() {
    echo -e "${cyan}>>> 远程桌面重复登录修复${reset}"
    sudo sed -i 's/port=3389/port=tcp:3389/g' /etc/xrdp/xrdp.ini
    sudo systemctl restart xrdp
    echo -e "${green}[通知] 远程桌面服务已修复${reset}"
}

# U盘/外设弹窗修复：禁用自动挂载和自动打开
fix_usb_device_popup() {
    echo -e "${cyan}>>> 外设/U盘弹窗修复${reset}"
    gsettings set org.ukui.settings-daemon.plugins.media-helper automount false
    gsettings set org.ukui.settings-daemon.plugins.media-helper automount-open false
    echo -e "${green}[通知] U盘自动挂载已禁用${reset}"
}

# WPS打印时显示多个打印机的问题修复
fix_wps_multiple_printers() {
    echo -e "${cyan}>>> WPS打印时显示多个打印机修复${reset}"
    # 关闭avahi-daemon服务
    sudo  systemctl   stop   avahi-daemon.socket   avahi-daemon.service 
    # 关闭avahi-daemon服务开机自启动
    sudo  systemctl   disable   avahi-daemon.socket   avahi-daemon.service
    echo -e "${green}[通知] 已修复WPS打印时显示多个打印机${reset}"
}

# 解决WPS运行慢或卡的问题
fix_wps_performance() {
    echo -e "${cyan}>>> 解决WPS运行慢或卡的问题${reset}"
          echo -e "${green}[通知] 正在备份数据${reset}"
          sudo cp -rf    ${HOME}/.config/Kingsoft    ${HOME}/.config/Kingsoft_bak
          sudo cp -rf    ${HOME}/.config/kingsoft    ${HOME}/.config/kingsoft_bak
          sudo cp -rf    ${HOME}/.local/share/Kingsoft ${HOME}/.local/share/Kingsoft_bak
          echo -e "${green}[通知] 备份数据完成 ${reset}"
          echo -e "${green}[通知] 正在处理WPS运行慢或卡的问题${reset}"
          sudo rm -rfv   ${HOME}/.config/Kingsoft
          sudo rm -rfv   ${HOME}/.config/kingsoft
         sudo rm -rfv   ${HOME}/.local/share/Kingsoft
    echo -e "${green}[通知] 已解决WPS运行慢或卡的问题${reset}"
}
# 执行主函数 
other_menu

# 添加循环保护，防止意外退出
if [ $? -ne 0 ]; then
    echo -e "${red}菜单执行出错，3秒后返回主菜单...${reset}"
    sleep 3
fi